﻿using System;
using System.Windows.Forms;
using System.Linq;
using Common;
using DataLibrary;
using System.Collections.Generic;
using System.Data;
using LinqKit;
using System.Drawing.Printing;
using System.Drawing;

namespace TicketSystem
{
    public partial class UC_Reports : UserControl
    {
        private TicketSystemEntities tse = new TicketSystemEntities();
        private PublicMethod pm = new PublicMethod();
        public UC_Reports()
        {
            InitializeComponent();
            InitprinterComboBox(); //初始化打印机下拉列表选项
        }
        public int ReportType;
        private void UC_Report_Load(object sender, EventArgs e)
        {            
            //InitData(ReportType);
            InitData();
            var _opes = tse.Operators.Select(u => new { u.OperatorID, u.Name }).ToList();
            cBox_Operator.DataSource = _opes;
            cBox_Operator.ValueMember = "OperatorID";
            cBox_Operator.DisplayMember = "Name";
            var _OpeSource = new AutoCompleteStringCollection();
            foreach (var _ope in _opes)
            {
                _OpeSource.Add(_ope.Name);
            }
            cBox_Operator.AutoCompleteCustomSource = _OpeSource;
            cBox_Operator.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            cBox_Operator.AutoCompleteSource = AutoCompleteSource.CustomSource;
            cBox_Operator.Text = "";

            int roleid = tse.Operators.Where(u => u.OperatorID == GetOperator.OperatorID).FirstOrDefault().RoleID;
            List<string> l = new List<string>();
            var right = tse.RoleRights.Where(u => u.RoieID == roleid);
            foreach (var r in right)
            {
                l.Add(r.Rights.RightName);
            }
            //验证操作员是否可以打印和导出导游信息
            pm.CanExport(this.btn_ExportAll, l);
            pm.CanPrint(this.btn_print, l);
        }

        //初始化

        private void InitData()
        {
            txt_count.Text = "";
            DataTable dt1 = (DataTable)rowMergeView_SaleAll.DataSource;
            DataTable dt2 = (DataTable)rowMergeView_Back.DataSource;
            DataTable dt_Signed = (DataTable)rowMergeView_Signed.DataSource;
            if (dt1 != null)
            {
                dt1.Rows.Clear();
                rowMergeView_SaleAll.DataSource = dt1;
            }
            if (dt2 != null)
            {
                dt2.Rows.Clear();
                rowMergeView_Back.DataSource = dt2;
            }
            if (dt_Signed != null)
            {
                dt_Signed.Rows.Clear();
                rowMergeView_Signed.DataSource = dt_Signed;
            }
            this.rowMergeView_SaleAll.Visible = true;
            this.cBox_ReportType.SelectedIndex = 0;
            this.cBox_TicketType.SelectedIndex = 0;
            this.rowMergeView_SaleAll.ColumnHeadersHeight = 40;
            this.rowMergeView_SaleAll.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.rowMergeView_SaleAll.MergeColumnNames.Add("Column1");
            this.rowMergeView_SaleAll.AddSpanHeader(4, 3, "首道船");
            this.rowMergeView_SaleAll.AddSpanHeader(7, 3, "公园票");
            this.rowMergeView_SaleAll.AddSpanHeader(10, 2, "颐内一道船");
            this.rowMergeView_SaleAll.AddSpanHeader(12, 2, "颐内二道船");

            this.rowMergeView_Back.ColumnHeadersHeight = 40;
            this.rowMergeView_Back.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.rowMergeView_Back.MergeColumnNames.Add("Column1");
            this.rowMergeView_Back.AddSpanHeader(4, 2, "首道船");
            this.rowMergeView_Back.AddSpanHeader(6, 3, "公园票");
            this.rowMergeView_Back.AddSpanHeader(9, 2, "颐内一道船");
            this.rowMergeView_Back.AddSpanHeader(11, 2, "颐内二道船");

            this.rowMergeView_Signed.ColumnHeadersHeight = 40;
            this.rowMergeView_Signed.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.rowMergeView_Signed.AddSpanHeader(4, 2, "首道船");

            this.rowMergeView_Unsigned.ColumnHeadersHeight = 40;
            this.rowMergeView_Unsigned.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            this.rowMergeView_Unsigned.MergeColumnNames.Add("Column1");
            this.rowMergeView_Unsigned.AddSpanHeader(3, 2, "首道船(非签单)");
            this.rowMergeView_Unsigned.AddSpanHeader(5, 3, "公园票");
            this.rowMergeView_Unsigned.AddSpanHeader(8, 2, "颐内一道船");
            this.rowMergeView_Unsigned.AddSpanHeader(10, 2, "颐内二道船");

            this.dataGridView1.DataSource = null;
            
        }

        private DataTable SalesAllTable(List<Sales> sales)
        {
            var ticketTypes = tse.TicketTypes.ToList();
            DataTable dt = new DataTable();
            for (int a = 0; a < rowMergeView_SaleAll.Columns.Count; a++)
            {
                dt.Columns.Add((rowMergeView_SaleAll.Columns[a].Index + 1).ToString());
            }
            for (int i = 0; i < sales.Count(); i++)
            {
                List<string> list = new List<string>(dt.Columns.Count);
                list.Add(sales[i].SaleID.ToString());
                list.Add((sales[i].DepartmentsGuides.Departments.DepartmentName.Trim().Length > 13 ? sales[i].DepartmentsGuides.Departments.DepartmentName.Trim().Substring(0, 13) + "..." : sales[i].DepartmentsGuides.Departments.DepartmentName.Trim()));
                //list.Add(sales[i].DepartmentsGuides.Guides.Name.Trim());
                list.Add(sales[i].DepartmentsGuides.Guides.Name.Trim());
                list.Add(sales[i].SignedNumber.ToString().Trim());
                list.Add(sales[i].TicketOne.ToString());
                list.Add(sales[i].SignedPeople.ToString());
                list.Add(Math.Round(sales[i].TicketOne * ticketTypes[0].Price, 2).ToString());
                list.Add(sales[i].TicketPark.ToString());
                list.Add(sales[i].TicketParkSpecial.ToString());
                list.Add(Math.Round(sales[i].TicketPark * ticketTypes[3].Price + sales[i].TicketParkSpecial * ticketTypes[3].SpecialPrice, 2).ToString());
                list.Add(sales[i].TicketTwo.ToString());
                list.Add(Math.Round(sales[i].TicketTwo * ticketTypes[1].Price, 2).ToString());
                list.Add(sales[i].TicketThree.ToString());
                list.Add(Math.Round(sales[i].TicketThree * ticketTypes[2].Price, 2).ToString());
                list.Add(Math.Round(sales[i].SignedMoney, 2).ToString());
                list.Add(Math.Round(sales[i].PurchaseMoney, 2).ToString());
                list.Add(Math.Round(sales[i].PurchaseMoney + sales[i].SignedMoney, 2).ToString());
                list.Add(sales[i].SaleGuid.ToString());
                list.Add(sales[i].Created_at.ToString());
                list.Add(sales[i].Checkout_at.ToString());
                dt.Rows.Add(list.ToArray());

            }
            System.Collections.Hashtable ht = new System.Collections.Hashtable();
            foreach (DataRow dr in dt.Rows)
            {
                ht["OneTotal"] = Convert.ToInt32(ht["OneTotal"]) + Convert.ToInt32(dr[4]);
                ht["SignedTotal"] = Convert.ToInt32(ht["SignedTotal"]) + Convert.ToInt32(dr[5]);
                ht["OneMoney"] = Math.Round(Convert.ToDecimal(ht["OneMoney"]) + Convert.ToDecimal(dr[6]), 2);
                ht["ParkNum"] = Convert.ToInt32(ht["ParkNum"]) + Convert.ToInt32(dr[7]);
                ht["ParkSpecialNum"] = Convert.ToInt32(ht["ParkSpecialNum"]) + Convert.ToInt32(dr[8]);
                ht["ParkMoney"] = Math.Round(Convert.ToDecimal(ht["ParkMoney"]) + Convert.ToDecimal(dr[9]), 2);
                ht["TwoTotal"] = Convert.ToInt32(ht["TwoTotal"]) + Convert.ToInt32(dr[10]);
                ht["TwoMoney"] = Math.Round(Convert.ToDecimal(ht["TwoMoney"]) + Convert.ToDecimal(dr[11]), 2);
                ht["ThreeTotal"] = Convert.ToInt32(ht["ThreeTotal"]) + Convert.ToInt32(dr[12]);
                ht["ThreeMoney"] = Math.Round(Convert.ToDecimal(ht["ThreeMoney"]) + Convert.ToDecimal(dr[13]), 2);
                ht["SignedTotalMoney"] = Math.Round(Convert.ToDecimal(ht["SignedTotalMoney"]) + Convert.ToDecimal(dr[14]), 2);
                ht["UnSignedTotalMoney"] = Math.Round(Convert.ToDecimal(ht["UnSignedTotalMoney"]) + Convert.ToDecimal(dr[15]), 2);
                ht["TotalMoney"] = Math.Round(Convert.ToDecimal(ht["TotalMoney"]) + Convert.ToDecimal(dr[16]), 2);
            }
            dt.Rows.Add("总计：", "", "", "", ht["OneTotal"], ht["SignedTotal"], ht["OneMoney"], ht["ParkNum"], ht["ParkSpecialNum"], ht["ParkMoney"], ht["TwoTotal"], ht["TwoMoney"], ht["ThreeTotal"], ht["ThreeMoney"], ht["SignedTotalMoney"], ht["UnSignedTotalMoney"], ht["TotalMoney"], "");
            DataTable dtTotal = new DataTable();
            dtTotal.Columns.Add("-");
            dtTotal.Columns.Add("首道船人数");
            dtTotal.Columns.Add("签单人数");
            dtTotal.Columns.Add("首道船金额");
            dtTotal.Columns.Add("进园成人数");
            dtTotal.Columns.Add("进园学生数");
            dtTotal.Columns.Add("进园金额");
            dtTotal.Columns.Add("颐内一道船");
            dtTotal.Columns.Add("颐内一道船金额");
            dtTotal.Columns.Add("颐内二道船");
            dtTotal.Columns.Add("颐内二道船金额");
            dtTotal.Columns.Add("签单金额");
            dtTotal.Columns.Add("非签单金额");
            dtTotal.Columns.Add("总金额");
            dtTotal.Rows.Add("总计：", ht["OneTotal"], ht["SignedTotal"], ht["OneMoney"], ht["ParkNum"], ht["ParkSpecialNum"], ht["ParkMoney"], ht["TwoTotal"], ht["TwoMoney"], ht["ThreeTotal"], ht["ThreeMoney"], ht["SignedTotalMoney"], ht["UnSignedTotalMoney"], ht["TotalMoney"]);
            this.dataGridView1.DataSource = dtTotal;
            return dt;
        }

        private DataTable SalesUnsignedTable(List<Sales> sales)
        {
            var ticketTypes = tse.TicketTypes.ToList();
            DataTable dt = new DataTable();
            for (int a = 0; a < rowMergeView_Unsigned.Columns.Count; a++)
            {
                dt.Columns.Add((rowMergeView_Unsigned.Columns[a].Index + 1).ToString());
            }
            for (int i = 0; i < sales.Count(); i++)
            {
                List<string> list = new List<string>(dt.Columns.Count);
                list.Add(sales[i].SaleID.ToString());
                list.Add((sales[i].DepartmentsGuides.Departments.DepartmentName.Trim().Length > 13 ? sales[i].DepartmentsGuides.Departments.DepartmentName.Trim().Substring(0, 13) + "..." : sales[i].DepartmentsGuides.Departments.DepartmentName.Trim()));
                list.Add(sales[i].DepartmentsGuides.Guides.Name.Trim());
                list.Add((sales[i].TicketOne - sales[i].SignedPeople).ToString());
                list.Add(Math.Round((sales[i].TicketOne - sales[i].SignedPeople) * ticketTypes[0].Price, 2).ToString());
                list.Add(sales[i].TicketPark.ToString());
                list.Add(sales[i].TicketParkSpecial.ToString());
                list.Add(Math.Round(sales[i].TicketPark * ticketTypes[3].Price + sales[i].TicketParkSpecial * ticketTypes[3].SpecialPrice, 2).ToString());
                list.Add(sales[i].TicketTwo.ToString());
                list.Add(Math.Round(sales[i].TicketTwo * ticketTypes[1].Price, 2).ToString());
                list.Add(sales[i].TicketThree.ToString());
                list.Add(Math.Round(sales[i].TicketThree * ticketTypes[2].Price, 2).ToString());
                list.Add(Math.Round(sales[i].PurchaseMoney, 2).ToString());
                list.Add(sales[i].SaleGuid.ToString());
                list.Add(sales[i].Created_at.ToString());
                list.Add(sales[i].Checkout_at.ToString());
                dt.Rows.Add(list.ToArray());
            }
            System.Collections.Hashtable ht = new System.Collections.Hashtable();
            foreach (DataRow dr in dt.Rows)
            {
                ht["OneTotal"] = Convert.ToInt32(ht["OneTotal"]) + Convert.ToInt32(dr[3]);
                ht["OneMoney"] = Math.Round(Convert.ToDecimal(ht["OneMoney"]) + Convert.ToDecimal(dr[4]), 2);
                ht["ParkNum"] = Convert.ToInt32(ht["ParkNum"]) + Convert.ToInt32(dr[5]);
                ht["ParkSpecialNum"] = Convert.ToInt32(ht["ParkSpecialNum"]) + Convert.ToInt32(dr[6]);
                ht["ParkMoney"] = Math.Round(Convert.ToDecimal(ht["ParkMoney"]) + Convert.ToDecimal(dr[7]), 2);
                ht["TwoTotal"] = Convert.ToInt32(ht["TwoTotal"]) + Convert.ToInt32(dr[8]);
                ht["TwoMoney"] = Math.Round(Convert.ToDecimal(ht["TwoMoney"]) + Convert.ToDecimal(dr[9]), 2);
                ht["ThreeTotal"] = Convert.ToInt32(ht["ThreeTotal"]) + Convert.ToInt32(dr[10]);
                ht["ThreeMoney"] = Math.Round(Convert.ToDecimal(ht["ThreeMoney"]) + Convert.ToDecimal(dr[11]), 2);
                ht["UnSignedTotalMoney"] = Math.Round(Convert.ToDecimal(ht["UnSignedTotalMoney"]) + Convert.ToDecimal(dr[12]), 2);
            }
            dt.Rows.Add("总计：", "", "", ht["OneTotal"], ht["OneMoney"], ht["ParkNum"], ht["ParkSpecialNum"], ht["ParkMoney"], ht["TwoTotal"], ht["TwoMoney"], ht["ThreeTotal"], ht["ThreeMoney"], ht["UnSignedTotalMoney"]);
            DataTable dtTotal = new DataTable();
            dtTotal.Columns.Add("-");
            dtTotal.Columns.Add("首道船人数");
            dtTotal.Columns.Add("首道船金额");
            dtTotal.Columns.Add("进园成人数");
            dtTotal.Columns.Add("进园学生数");
            dtTotal.Columns.Add("进园金额");
            dtTotal.Columns.Add("颐内一道船");
            dtTotal.Columns.Add("颐内一道船金额");
            dtTotal.Columns.Add("颐内二道船");
            dtTotal.Columns.Add("颐内二道船金额");
            dtTotal.Columns.Add("非签单总金额");
            dtTotal.Rows.Add("总计：", ht["OneTotal"], ht["OneMoney"], ht["ParkNum"], ht["ParkSpecialNum"], ht["ParkMoney"], ht["TwoTotal"], ht["TwoMoney"], ht["ThreeTotal"], ht["ThreeMoney"], ht["UnSignedTotalMoney"]);
            this.dataGridView1.DataSource = dtTotal;
            return dt;
        }

        private DataTable SalesSignedTable(List<Sales> sales)
        {
            var ticketTypes = tse.TicketTypes.ToList();
            DataTable dt = new DataTable();
            for (int a = 0; a < rowMergeView_Signed.Columns.Count; a++)
            {
                dt.Columns.Add((rowMergeView_Signed.Columns[a].Index + 1).ToString());
            }
            for (int i = 0; i < sales.Count(); i++)
            {
                List<string> list = new List<string>(dt.Columns.Count);
                list.Add(sales[i].SaleID.ToString());
                list.Add((sales[i].DepartmentsGuides.Departments.DepartmentName.Trim().Length > 13 ? sales[i].DepartmentsGuides.Departments.DepartmentName.Trim().Substring(0, 13) + "..." : sales[i].DepartmentsGuides.Departments.DepartmentName.Trim()));
                list.Add(sales[i].DepartmentsGuides.Guides.Name.Trim());
                list.Add(sales[i].SignedNumber.ToString().Trim());
                list.Add(sales[i].TicketOne.ToString());
                list.Add(sales[i].SignedPeople.ToString());
                list.Add(Math.Round(sales[i].SignedMoney, 2).ToString());
                list.Add(sales[i].SaleGuid.ToString());
                list.Add(sales[i].Created_at.ToString());
                list.Add(sales[i].Checkout_at.ToString());
                dt.Rows.Add(list.ToArray());
            }
            System.Collections.Hashtable ht = new System.Collections.Hashtable();
            foreach (DataRow dr in dt.Rows)
            {
                ht["OneTotal"] = Convert.ToInt32(ht["OneTotal"]) + Convert.ToInt32(dr[4]);
                ht["SignedTotal"] = Convert.ToInt32(ht["SignedTotal"]) + Convert.ToInt32(dr[5]);
                ht["SignedTotalMoney"] = Math.Round((Convert.ToDecimal(ht["SignedTotalMoney"]) + Convert.ToDecimal(dr[6])), 2);
            }
            dt.Rows.Add("总计：", "", "", "", ht["OneTotal"], ht["SignedTotal"], ht["SignedTotalMoney"], "");
            DataTable dtTotal = new DataTable();
            dtTotal.Columns.Add("-");
            dtTotal.Columns.Add("乘船人数");
            dtTotal.Columns.Add("签单人数");
            dtTotal.Columns.Add("签单金额");
            dtTotal.Rows.Add("总计：", ht["OneTotal"], ht["SignedTotal"], ht["SignedTotalMoney"]);
            this.dataGridView1.DataSource = dtTotal;
            return dt;
        }

        private DataTable BacksTable(List<Backs> backs)
        {
            var ticketTypes = tse.TicketTypes.ToList();
            DataTable dt = new DataTable();
            for (int a = 0; a < rowMergeView_Back.Columns.Count; a++)
            {
                dt.Columns.Add((rowMergeView_Back.Columns[a].Index + 1).ToString());
            }
            for (int i = 0; i < backs.Count(); i++)
            {
                List<string> list = new List<string>(dt.Columns.Count);
                list.Add(backs[i].BackID.ToString());
                list.Add(backs[i].SaleID.ToString());
                list.Add(backs[i].Sales.DepartmentsGuides.Departments.DepartmentName.Trim());
                list.Add(backs[i].Sales.DepartmentsGuides.Guides.Name.Trim());
                list.Add(backs[i].BackOne.ToString());
                list.Add(Math.Round(backs[i].BackOne * ticketTypes[0].Price, 2).ToString());
                list.Add(backs[i].BackPark.ToString());
                list.Add(backs[i].BackParkSpecial.ToString());
                list.Add(Math.Round(backs[i].BackPark * ticketTypes[3].Price + backs[i].BackParkSpecial * ticketTypes[3].SpecialPrice, 2).ToString());
                list.Add(backs[i].BackTwo.ToString());
                list.Add(Math.Round(backs[i].BackTwo * ticketTypes[1].Price, 2).ToString());
                list.Add(backs[i].BackThree.ToString());
                list.Add(Math.Round(backs[i].BackThree * ticketTypes[2].Price, 2).ToString());
                list.Add(Math.Round(backs[i].BackMoney, 2).ToString());
                list.Add(backs[i].Created_at.ToString());
                dt.Rows.Add(list.ToArray());

            }
            System.Collections.Hashtable ht = new System.Collections.Hashtable();
            foreach (DataRow dr in dt.Rows)
            {
                ht["BackOne"] = Convert.ToInt32(ht["BackOne"]) + Convert.ToInt32(dr[4]);
                ht["BackOneMoney"] = Math.Round(Convert.ToDecimal(ht["BackOneMoney"]) + Convert.ToDecimal(dr[5]), 2);
                ht["BackPark"] = Convert.ToInt32(ht["BackPark"]) + Convert.ToInt32(dr[6]);
                ht["BackParkSpecial"] = Convert.ToInt32(ht["BackParkSpecial"]) + Convert.ToInt32(dr[7]);
                ht["BackParkMoney"] = Math.Round(Convert.ToDecimal(ht["BackParkMoney"]) + Convert.ToDecimal(dr[8]), 2);
                ht["BackTwo"] = Convert.ToInt32(ht["BackTwo"]) + Convert.ToInt32(dr[9]);
                ht["BackTwoMoney"] = Math.Round(Convert.ToDecimal(ht["BackTwoMoney"]) + Convert.ToDecimal(dr[10]), 2);
                ht["BackThree"] = Convert.ToInt32(ht["BackThree"]) + Convert.ToInt32(dr[11]);
                ht["BackThreeMoney"] = Math.Round(Convert.ToDecimal(ht["BackThreeMoney"]) + Convert.ToDecimal(dr[12]), 2);
                ht["BackTotalMoney"] = Math.Round(Convert.ToDecimal(ht["BackTotalMoney"]) + Convert.ToDecimal(dr[13]), 2);
            }
            dt.Rows.Add("总计：", "", "", "", ht["BackOne"], ht["BackOneMoney"], ht["BackPark"], ht["BackParkSpecial"], ht["BackParkMoney"], ht["BackTwo"], ht["BackTwoMoney"], ht["BackThree"], ht["BackThreeMoney"], ht["BackTotalMoney"]);
            DataTable dtTotal = new DataTable();
            dtTotal.Columns.Add("-");
            dtTotal.Columns.Add("首道船人数");
            dtTotal.Columns.Add("首道船金额");
            dtTotal.Columns.Add("进园成人数");
            dtTotal.Columns.Add("进园学生数");
            dtTotal.Columns.Add("进园金额");
            dtTotal.Columns.Add("颐内一道船");
            dtTotal.Columns.Add("颐内一道船金额");
            dtTotal.Columns.Add("颐内二道船");
            dtTotal.Columns.Add("颐内二道船金额");
            dtTotal.Columns.Add("退单总金额");
            dtTotal.Rows.Add("总计：", ht["BackOne"], ht["BackOneMoney"], ht["BackPark"], ht["BackParkSpecial"], ht["BackParkMoney"], ht["BackTwo"], ht["BackTwoMoney"], ht["BackThree"], ht["BackThreeMoney"], ht["BackTotalMoney"]);
            this.dataGridView1.DataSource = dtTotal;
            return dt;
        }

        /// <summary>
        /// 将检索结果呈现，sals：结果集
        /// </summary>
        /// <param name="sals"></param>
        private void BindData(List<Sales> sales, List<Backs> backs)
        {
            txt_count.Text = "";
            if (sales != null && sales.Count > 0)
            {
                if (cBox_TicketType.Text == "全部")
                {
                    DataTable dt = SalesAllTable(sales);
                    this.rowMergeView_SaleAll.DataSource = dt;
                    rowMergeView_SaleAll.Columns[0].Frozen = true;
                    rowMergeView_SaleAll.Columns[1].Frozen = true;
                    rowMergeView_SaleAll.Columns[2].Frozen = true;
                }
                else if (cBox_TicketType.Text == "签单")
                {
                    DataTable dt = SalesSignedTable(sales);
                    this.rowMergeView_Signed.DataSource = dt;
                    rowMergeView_Signed.Columns[0].Frozen = true;
                    rowMergeView_Signed.Columns[1].Frozen = true;
                    rowMergeView_Signed.Columns[2].Frozen = true;
                }
                else if (cBox_TicketType.Text == "非签单")
                {
                    DataTable dt = SalesUnsignedTable(sales);
                    this.rowMergeView_Unsigned.DataSource = dt;
                    rowMergeView_Unsigned.Columns[0].Frozen = true;
                    rowMergeView_Unsigned.Columns[1].Frozen = true;
                    rowMergeView_Unsigned.Columns[2].Frozen = true;
                }
            }
            else if (backs != null && backs.Count > 0)
            {
                DataTable dt = BacksTable(backs);
                this.rowMergeView_Back.DataSource = dt;
                rowMergeView_Back.Columns[0].Frozen = true;
                rowMergeView_Back.Columns[1].Frozen = true;
                rowMergeView_Back.Columns[2].Frozen = true;
            }
            else
            {
                MessageBox.Show("检索结果为空");
                InitData();
            }

        }

        private void btn_Search_Click(object sender, EventArgs e)
        {
            DateTime start;
            DateTime end;
            if (checkBox_TimeSpan.Checked)
            {
                start = Convert.ToDateTime(dTP_Start.Text);
                end = Convert.ToDateTime(dTP_End.Text);
            }
            else
            {
                start = Convert.ToDateTime(Convert.ToDateTime(dTP_Start.Text).ToString("yyyy-MM-dd ") + " 00:00:00");
                end = Convert.ToDateTime(Convert.ToDateTime(dTP_End.Text).ToString("yyyy-MM-dd ") + " 23:59:59");
            }
            var _where = PredicateBuilder.New<Sales>(true);//出票单条件
            var _whereBack = PredicateBuilder.New<Backs>(true);//退票单条件
            List<Sales> sales = null;
            List<Backs> backs = null;
            if (checkBox_CheckOut.Checked)
            {
                _where = _where.And(u => u.Checkout_at >= start && u.Checkout_at <= end);
            }
            else
            {
                _where = _where.And(u => u.Created_at >= start && u.Created_at <= end);
            }

            _whereBack = _whereBack.And(u => u.Created_at >= start && u.Created_at <= end);
            if (!string.IsNullOrEmpty(cBox_Operator.Text))
            {
                int operatorid = (int)cBox_Operator.SelectedValue;
                _where = _where.And(u => u.OperatorID == operatorid);
                _whereBack = _whereBack.And(u => u.OperatorID == operatorid);
            }
            if (!string.IsNullOrEmpty(txt_Depart.Text))
            {
                _where = _where.And(u => u.DepartmentsGuides.Departments.DepartmentName == txt_Depart.Text);
                _whereBack = _whereBack.And(u => u.Sales.DepartmentsGuides.Departments.DepartmentName.Trim() == txt_Depart.Text);
            }
            if (!string.IsNullOrEmpty(txt_Guide.Text))
            {
                _where = _where.And(u => u.DepartmentsGuides.Guides.Name == txt_Guide.Text);
                _whereBack = _whereBack.And(u => u.Sales.DepartmentsGuides.Guides.Name.Trim() == txt_Guide.Text);
            }
            switch (cBox_TicketType.Text)
            {
                case "全部":
                    break;
                case "签单":
                    _where = _where.And(u => u.SignedMoney > 0 && u.BackFlag == 0);
                    break;
                case "非签单":
                    _where = _where.And(u=>u.PurchaseMoney > 0 && u.BackFlag == 0);
                    break;
                default:
                    break;
            }
            if (cBox_ReportType.Text == "退票报表")
            {
                backs = tse.Backs.AsExpandable().Where(_whereBack).ToList();
                BindData(null, backs);
                txt_count.Text = backs.Count().ToString() + "条";
            }
            else
            {
                sales = tse.Sales.AsExpandable().Where(_where).ToList();
                BindData(sales, null);
                txt_count.Text = sales.Count().ToString() + "条";
            }
        }

        private void cBox_ReportType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cBox_ReportType.Text == "退票报表")
            {
                checkBox_CheckOut.Enabled = false;
                cBox_TicketType.Enabled = false;
                rowMergeView_SaleAll.Visible = false;
                rowMergeView_Signed.Visible = false;
                rowMergeView_Back.Visible = true;
            }
            else
            {
                checkBox_CheckOut.Enabled = true;
                cBox_TicketType.Enabled = true;
                rowMergeView_Back.Visible = false;
                rowMergeView_Signed.Visible = false;
                rowMergeView_SaleAll.Visible = true;
            }
        }

        private void cBox_TicketType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cBox_TicketType.Text == "全部")
            {
                rowMergeView_SaleAll.Visible = true;
                rowMergeView_Signed.Visible = false;
                rowMergeView_Unsigned.Visible = false;
            }
            else if (cBox_TicketType.Text == "签单")
            {
                rowMergeView_SaleAll.Visible = false;
                rowMergeView_Signed.Visible = true;
                rowMergeView_Unsigned.Visible = false;
            }
            else if (cBox_TicketType.Text == "非签单")
            {
                rowMergeView_SaleAll.Visible = false;
                rowMergeView_Signed.Visible = false;
                rowMergeView_Unsigned.Visible = true;
            }
        }

        private void btn_print_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(printerComboBox.Text))
            {
                MessageBox.Show("请先选择默认打印机");
                return;
            }
            else
            {
                var printPreviewDialog = new PrintPreviewDialog { Document = printDocument1 };
                printPreviewDialog.ShowDialog();
                return;
            }
        }

        private DataGridView SalesDgv()
        {
            int PerPageOneTotal = 0;
            int PerPageSignedTotal = 0;
            int PerPageParkTotal = 0;
            int PerPageParkSpecialTotal = 0;
            int PerPageTwoTotal = 0;
            int PerPageThreeTotal = 0;
            decimal PerPageMoneyTotal = 0;
            decimal PerPageSignedMoney = 0;
            decimal PerPageUnSignedMoney = 0;
            //decimal PerPageBackedMoney = 0;
            DataGridView dgv = new DataGridView();
            dgv.Columns.Add("1", "流水号");
            dgv.Columns.Add("2", "社名");
            dgv.Columns.Add("3", "导游");
            dgv.Columns.Add("4", "签单号");
            dgv.Columns.Add("5", "非签单人数");
            dgv.Columns.Add("6", "签单人数");
            dgv.Columns.Add("7", "进园成人票");
            dgv.Columns.Add("8", "进园学生票");
            dgv.Columns.Add("9", "颐内一道船");
            dgv.Columns.Add("10", "颐内二道船");
            dgv.Columns.Add("11", "签单金额");
            dgv.Columns.Add("12", "非签单金额");
            dgv.Columns.Add("13", "总金额");
            
            if (rowMergeView_SaleAll.Rows.Count > 0)
            {
                DataTable datatable = new DataTable();
                datatable = (DataTable)rowMergeView_SaleAll.DataSource;
                int cnt = 0;
                foreach (DataRow dr in datatable.Rows)
                {
                    int index = dgv.Rows.Add();
                    dgv.Rows[index].Cells[0].Value = dr[0];
                    dgv.Rows[index].Cells[1].Value = dr[1];
                    dgv.Rows[index].Cells[2].Value = dr[2];
                    dgv.Rows[index].Cells[3].Value = dr[3];
                    dgv.Rows[index].Cells[4].Value = Convert.ToInt32(dr[4]) - Convert.ToInt32(dr[5]);
                    dgv.Rows[index].Cells[5].Value = dr[5];
                    dgv.Rows[index].Cells[6].Value = dr[7];
                    dgv.Rows[index].Cells[7].Value = dr[8];
                    dgv.Rows[index].Cells[8].Value = dr[10];
                    dgv.Rows[index].Cells[9].Value = dr[12];
                    dgv.Rows[index].Cells[10].Value = dr[14];
                    dgv.Rows[index].Cells[11].Value = dr[15];
                    dgv.Rows[index].Cells[12].Value = dr[16];
                    if (cnt < datatable.Rows.Count)
                    {
                        PerPageOneTotal += int.Parse(dgv.Rows[index].Cells[4].Value.ToString());
                        PerPageSignedTotal += int.Parse(dgv.Rows[index].Cells[5].Value.ToString());
                        PerPageParkTotal += int.Parse(dgv.Rows[index].Cells[6].Value.ToString());
                        PerPageParkSpecialTotal += int.Parse(dgv.Rows[index].Cells[7].Value.ToString());
                        PerPageTwoTotal += int.Parse(dgv.Rows[index].Cells[8].Value.ToString());
                        PerPageThreeTotal += int.Parse(dgv.Rows[index].Cells[9].Value.ToString());
                        PerPageSignedMoney += decimal.Parse(dgv.Rows[index].Cells[10].Value.ToString());
                        PerPageUnSignedMoney += decimal.Parse(dgv.Rows[index].Cells[11].Value.ToString());
                        PerPageMoneyTotal += decimal.Parse(dgv.Rows[index].Cells[12].Value.ToString());
                        cnt++;
                        if (cnt % 20 == 0)
                        {
                            dgv.Rows.Add("小计", "", "", "", PerPageOneTotal, PerPageSignedTotal, PerPageParkTotal, PerPageParkSpecialTotal, PerPageTwoTotal, PerPageThreeTotal, Math.Round(PerPageSignedMoney, 2), Math.Round(PerPageUnSignedMoney, 2), Math.Round(PerPageMoneyTotal, 2));
                            PerPageMoneyTotal = 0; PerPageOneTotal = 0; PerPageParkSpecialTotal = 0; PerPageParkTotal = 0; PerPageSignedMoney = 0;
                            PerPageSignedTotal = 0; PerPageThreeTotal = 0; PerPageTwoTotal = 0; PerPageUnSignedMoney = 0;
                        }
                        if (cnt == datatable.Rows.Count - 1)
                        {
                            dgv.Rows.Add("小计", "", "", "", PerPageOneTotal, PerPageSignedTotal, PerPageParkTotal, PerPageParkSpecialTotal, PerPageTwoTotal, PerPageThreeTotal, Math.Round(PerPageSignedMoney, 2), Math.Round(PerPageUnSignedMoney, 2), Math.Round(PerPageMoneyTotal, 2));
                            PerPageMoneyTotal = 0; PerPageOneTotal = 0; PerPageParkSpecialTotal = 0; PerPageParkTotal = 0; PerPageSignedMoney = 0;
                            PerPageSignedTotal = 0; PerPageThreeTotal = 0; PerPageTwoTotal = 0; PerPageUnSignedMoney = 0;
                        }
                    }
                }
            }
            return dgv;
        }

        private DataGridView SalesUnsignedDgv()
        {
            int PerPageOneTotal = 0;
            int PerPageParkTotal = 0;
            int PerPageParkSpecialTotal = 0;
            int PerPageTwoTotal = 0;
            int PerPageThreeTotal = 0;
            decimal PerPageUnSignedMoney = 0;
            DataGridView dgv = new DataGridView();
            dgv.Columns.Add("1", "流水号");
            dgv.Columns.Add("2", "社名");
            dgv.Columns.Add("3", "导游");
            dgv.Columns.Add("4", "首道船人数");
            dgv.Columns.Add("5", "进园成人票");
            dgv.Columns.Add("6", "进园学生票");
            dgv.Columns.Add("7", "颐内一道船");
            dgv.Columns.Add("8", "颐内二道船");
            dgv.Columns.Add("9", "非签单金额");
            if (rowMergeView_Unsigned.Rows.Count > 0)
            {
                DataTable datatable = new DataTable();
                datatable = (DataTable)rowMergeView_Unsigned.DataSource;
                int cnt = 0;
                foreach (DataRow dr in datatable.Rows)
                {
                    int index = dgv.Rows.Add();
                    dgv.Rows[index].Cells[0].Value = dr[0];
                    dgv.Rows[index].Cells[1].Value = dr[1];
                    dgv.Rows[index].Cells[2].Value = dr[2];
                    dgv.Rows[index].Cells[3].Value = dr[3];
                    dgv.Rows[index].Cells[4].Value = dr[5];
                    dgv.Rows[index].Cells[5].Value = dr[6];
                    dgv.Rows[index].Cells[6].Value = dr[8];
                    dgv.Rows[index].Cells[7].Value = dr[10];
                    dgv.Rows[index].Cells[8].Value = dr[12];
                    if (cnt < datatable.Rows.Count)
                    {
                        PerPageOneTotal += int.Parse(dgv.Rows[index].Cells[3].Value.ToString());
                        PerPageParkTotal += int.Parse(dgv.Rows[index].Cells[4].Value.ToString());
                        PerPageParkSpecialTotal += int.Parse(dgv.Rows[index].Cells[5].Value.ToString());
                        PerPageTwoTotal += int.Parse(dgv.Rows[index].Cells[6].Value.ToString());
                        PerPageThreeTotal += int.Parse(dgv.Rows[index].Cells[7].Value.ToString());
                        PerPageUnSignedMoney += decimal.Parse(dgv.Rows[index].Cells[8].Value.ToString());
                        cnt++;
                        if (cnt % 20 == 0)
                        {
                            dgv.Rows.Add("小计", "", "", PerPageOneTotal, PerPageParkTotal, PerPageParkSpecialTotal, PerPageTwoTotal, PerPageThreeTotal, Math.Round(PerPageUnSignedMoney, 2));
                            PerPageOneTotal = 0; PerPageParkSpecialTotal = 0; PerPageParkTotal = 0;
                            PerPageThreeTotal = 0; PerPageTwoTotal = 0; PerPageUnSignedMoney = 0;
                        }
                        if (cnt == datatable.Rows.Count - 1)
                        {
                            dgv.Rows.Add("小计", "", "", PerPageOneTotal, PerPageParkTotal, PerPageParkSpecialTotal, PerPageTwoTotal, PerPageThreeTotal, Math.Round(PerPageUnSignedMoney, 2));
                            PerPageOneTotal = 0; PerPageParkSpecialTotal = 0; PerPageParkTotal = 0;
                            PerPageThreeTotal = 0; PerPageTwoTotal = 0; PerPageUnSignedMoney = 0;
                        }
                    }
                }
            }
            return dgv;
        }

        private DataGridView SalesSignedDgv()
        {
            int PerPageOneTotal = 0;
            int PerPageSignedTotal = 0;
            decimal PerPageSignedMoney = 0;
            DataGridView dgv = new DataGridView();
            dgv.Columns.Add("1", "流水号");
            dgv.Columns.Add("2", "社名");
            dgv.Columns.Add("3", "导游");
            dgv.Columns.Add("4", "签单号");
            dgv.Columns.Add("5", "乘船人数");
            dgv.Columns.Add("6", "签单人数");
            dgv.Columns.Add("7", "签单金额");
            //dgv.Columns.Add("8", "票号");
            
            if (rowMergeView_Signed.Rows.Count > 0)
            {
                DataTable datatable = new DataTable();
                datatable = (DataTable)rowMergeView_Signed.DataSource;
                int cnt = 0;
                foreach (DataRow dr in datatable.Rows)
                {
                    int index = dgv.Rows.Add();
                    dgv.Rows[index].Cells[0].Value = dr[0];
                    dgv.Rows[index].Cells[1].Value = dr[1];
                    dgv.Rows[index].Cells[2].Value = dr[2];
                    dgv.Rows[index].Cells[3].Value = dr[3];
                    dgv.Rows[index].Cells[4].Value = dr[4];
                    dgv.Rows[index].Cells[5].Value = dr[5];
                    dgv.Rows[index].Cells[6].Value = dr[6];
              //      dgv.Rows[index].Cells[7].Value = dr[7];

                    if (cnt < datatable.Rows.Count)
                    {
                        PerPageOneTotal += int.Parse(dgv.Rows[index].Cells[4].Value.ToString());
                        PerPageSignedTotal += int.Parse(dgv.Rows[index].Cells[5].Value.ToString());
                        PerPageSignedMoney += decimal.Parse(dgv.Rows[index].Cells[6].Value.ToString());
                        cnt++;
                    }
                    if (cnt % 20 == 0)
                    {
                        dgv.Rows.Add("小计", "", "", "", PerPageOneTotal, PerPageSignedTotal, Math.Round(PerPageSignedMoney, 2));
                        PerPageOneTotal = 0; PerPageSignedMoney = 0; PerPageSignedTotal = 0;
                    }
                    if (cnt == datatable.Rows.Count - 1)
                    {
                        dgv.Rows.Add("小计", "", "", "", PerPageOneTotal, PerPageSignedTotal, Math.Round(PerPageSignedMoney, 2));
                        PerPageOneTotal = 0; PerPageSignedMoney = 0; PerPageSignedTotal = 0;
                    }

                }
            }
            return dgv;
        }

        private DataGridView BacksDgv()
        {
            int PerPageOneTotal = 0;
            int PerPageParkTotal = 0;
            int PerPageParkSpecialTotal = 0;
            int PerPageTwoTotal = 0;
            int PerPageThreeTotal = 0;
            decimal PerPageBackedTotal = 0;
            DataGridView dgv = new DataGridView();
            dgv.Columns.Add("1", "ID");
            dgv.Columns.Add("2", "原始流水号");
            dgv.Columns.Add("3", "社名");
            dgv.Columns.Add("4", "导游");
            dgv.Columns.Add("5", "首道船人数");
            dgv.Columns.Add("6", "进园成人票");
            dgv.Columns.Add("7", "进园学生票");
            dgv.Columns.Add("8", "颐内一道船");
            dgv.Columns.Add("9", "颐内二道船");
            dgv.Columns.Add("10", "退单总金额");
            if (rowMergeView_Back.Rows.Count > 0)
            {
                DataTable datatable = new DataTable();
                datatable = (DataTable)rowMergeView_Back.DataSource;
                int cnt = 0;
                foreach (DataRow dr in datatable.Rows)
                {
                    int index = dgv.Rows.Add();
                    dgv.Rows[index].Cells[0].Value = dr[0];
                    dgv.Rows[index].Cells[1].Value = dr[1];
                    dgv.Rows[index].Cells[2].Value = dr[2];
                    dgv.Rows[index].Cells[3].Value = dr[3];
                    dgv.Rows[index].Cells[4].Value = dr[4];
                    dgv.Rows[index].Cells[5].Value = dr[6];
                    dgv.Rows[index].Cells[6].Value = dr[7];
                    dgv.Rows[index].Cells[7].Value = dr[9];
                    dgv.Rows[index].Cells[8].Value = dr[11];
                    dgv.Rows[index].Cells[9].Value = dr[13];
                    if (cnt < datatable.Rows.Count)
                    {
                        PerPageOneTotal += int.Parse(dgv.Rows[index].Cells[4].Value.ToString());
                        PerPageParkTotal += int.Parse(dgv.Rows[index].Cells[5].Value.ToString());
                        PerPageParkSpecialTotal += int.Parse(dgv.Rows[index].Cells[6].Value.ToString());
                        PerPageTwoTotal += int.Parse(dgv.Rows[index].Cells[7].Value.ToString());
                        PerPageThreeTotal += int.Parse(dgv.Rows[index].Cells[8].Value.ToString());
                        PerPageBackedTotal += decimal.Parse(dgv.Rows[index].Cells[9].Value.ToString());
                        cnt++;
                        if (cnt % 20 == 0)
                        {
                            dgv.Rows.Add("小计", "", "", "", PerPageOneTotal, PerPageParkTotal, PerPageParkSpecialTotal, PerPageTwoTotal, PerPageThreeTotal, Math.Round(PerPageBackedTotal, 2));
                            PerPageOneTotal = 0; PerPageParkSpecialTotal = 0; PerPageParkTotal = 0;
                            PerPageThreeTotal = 0; PerPageTwoTotal = 0; PerPageBackedTotal = 0;
                        }
                        if (cnt == datatable.Rows.Count - 1)
                        {
                            dgv.Rows.Add("小计", "", "", "", PerPageOneTotal, PerPageParkTotal, PerPageParkSpecialTotal, PerPageTwoTotal, PerPageThreeTotal, Math.Round(PerPageBackedTotal, 2));
                            PerPageOneTotal = 0; PerPageParkSpecialTotal = 0; PerPageParkTotal = 0;
                            PerPageThreeTotal = 0; PerPageTwoTotal = 0; PerPageBackedTotal = 0;
                        }
                    }
                }
            }
            return dgv;
        }


        //构造打印数据
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            string start = "";
            string end = "";
            if (checkBox_TimeSpan.Checked)
            {
                start = dTP_Start.Value.ToString("yyyy-MM-dd HH:mm:ss");
                end = dTP_End.Value.ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                start = dTP_Start.Value.ToString("yyyy-MM-dd");
                end = dTP_End.Value.ToString("yyyy-MM-dd");
            }

            if (cBox_ReportType.Text == "出票报表")
            {
                if (cBox_TicketType.Text == "全部")
                {
                    DataGridView dgvsale = SalesDgv();
                    if (checkbox_TotalPrint.Checked)
                    {
                        Form_PartPrint fp = new Form_PartPrint(dgvsale, tse.Operators.Find(GetOperator.OperatorID).Name, start, end, "全部");
                        fp.ShowDialog();
                    }
                    else
                    {
                        Common.PrintDataGridView.Print(dgvsale, "日出票报表明细(全部)", "制表人：" + tse.Operators.Find(GetOperator.OperatorID).Name + "，开票时间:" + start + "——" + end, 0, 0);
                    }
                }
                else if (cBox_TicketType.Text == "签单")
                {
                    DataGridView dgvsalesign = SalesSignedDgv();
                    if (checkbox_TotalPrint.Checked)
                    {
                        Form_PartPrint fp = new Form_PartPrint(dgvsalesign, tse.Operators.Find(GetOperator.OperatorID).Name, start, end, "签单");
                        fp.ShowDialog();
                    }
                    else
                    {
                        Common.PrintDataGridView.Print(dgvsalesign, "日出票报表明细(签单)", "制表人：" + tse.Operators.Find(GetOperator.OperatorID).Name + "，开票时间:" + start + "——" + end, 0, 0);
                    }
                }
                else if (cBox_TicketType.Text == "非签单")
                {
                    DataGridView dgvsaleunsigned = SalesUnsignedDgv();
                    if (checkbox_TotalPrint.Checked)
                    {
                        Form_PartPrint fp = new Form_PartPrint(dgvsaleunsigned, tse.Operators.Find(GetOperator.OperatorID).Name, start, end, "非签单");
                        fp.ShowDialog();
                    }
                    else
                    {
                        Common.PrintDataGridView.Print(dgvsaleunsigned, "日出票报表明细(非签单)", "制表人：" + tse.Operators.Find(GetOperator.OperatorID).Name + "，开票时间:" + start + "——" + end, 0, 0);
                    }
                }
            }
            else
            {
                DataGridView dgvback = BacksDgv();
                if (checkbox_TotalPrint.Checked)
                {
                    Form_PartPrint fp = new Form_PartPrint(dgvback, tse.Operators.Find(GetOperator.OperatorID).Name, start, end, "退票");
                    fp.ShowDialog();
                }
                else
                {
                    Common.PrintDataGridView.Print(dgvback, "日退票报表明细(" + (string.IsNullOrEmpty(cBox_TicketType.Text) ? "全部" : cBox_TicketType.Text) + ")", "制表人：" + tse.Operators.Find(GetOperator.OperatorID).Name + "，开票时间:" + start + "——" + end, 0, 0);
                }
            }
        }

        private void btn_ExportAll_Click(object sender, EventArgs e)
        {
            if (cBox_ReportType.Text == "退票报表")
                ExportBacks();
            else
            {
                if (cBox_TicketType.Text == "全部")
                    ExportSales();
                else if (cBox_TicketType.Text == "签单")
                    ExportSalesSigned();
                else if (cBox_TicketType.Text == "非签单")
                    ExportSalesUnsigned();

            }
        }

        private void ExportSales()
        {
            DataTable dt = (DataTable)rowMergeView_SaleAll.DataSource;
            if (dt == null || dt.Rows.Count == 0)
            {
                MessageBox.Show("无数据");
            }
            else
            {
                System.Web.UI.WebControls.TableCell[] header = new System.Web.UI.WebControls.TableCell[26];
                for (int i = 0; i < header.Length; i++)
                {
                    header[i] = new System.Web.UI.WebControls.TableCell();
                }
                header[0].ColumnSpan = 6;
                header[0].Text = "";
                header[1].ColumnSpan = 3;
                header[1].Text = "首道船";
                header[2].ColumnSpan = 3;
                header[2].Text = "公园票";
                header[3].ColumnSpan = 2;
                header[3].Text = "颐内一道船";
                header[4].ColumnSpan = 2;
                header[4].Text = "颐内二道船";
                header[5].ColumnSpan = 4;
                header[5].Text = "</th><tr>";

                //第二行
                header[6].Text = "开票时间";
                header[7].Text = "结账时间";
                header[8].Text = "流水号";
                header[9].Text = "社名";
                header[10].Text = "导游";
                header[11].Text = "签单号";

                header[12].Text = "人数";
                header[13].Text = "签单";
                header[14].Text = "金额";

                header[15].Text = "普通票";
                header[16].Text = "学生票";
                header[17].Text = "金额";

                header[18].Text = "人数";
                header[19].Text = "金额";

                header[20].Text = "人数";
                header[21].Text = "金额";

                header[22].Text = "签单金额";
                header[23].Text = "非签单金额";
                header[24].Text = "总金额";
                header[25].Text = "单号";
                

                Dictionary<int, int> mergeCellNums = new Dictionary<int, int>();
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    mergeCellNums.Add(i, 2);
                }
                string filename = "";
                saveFileDialog1.FileName = DateTime.Now.ToString("yyyyMMdd-HHmmss") + "-日统计出票(全部)报表.xlsx";
                saveFileDialog1.Filter = " xls files(*.xls)|*.xlsx|All files(*.*)|*.*";
                saveFileDialog1.FilterIndex = 2;
                saveFileDialog1.RestoreDirectory = true;
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    filename = saveFileDialog1.FileName.ToString();
                    try
                    {
                        dt.Columns[18].SetOrdinal(0);
                        dt.Columns[19].SetOrdinal(1);
                        ToExcel.DataTable2Excel(dt, header, filename, mergeCellNums, 0);
                        MessageBox.Show("导出成功!");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                    }
                }
            }
        }

        private void ExportSalesUnsigned()
        {
            DataTable dt = (DataTable)rowMergeView_Unsigned.DataSource;
                     
            if (dt == null || dt.Rows.Count == 0)
            {
                MessageBox.Show("无数据");
            }
            else
            {
                System.Web.UI.WebControls.TableCell[] header = new System.Web.UI.WebControls.TableCell[22];
                for (int i = 0; i < header.Length; i++)
                {
                    header[i] = new System.Web.UI.WebControls.TableCell();
                }
                header[0].ColumnSpan = 5;
                header[0].Text = "";
                header[1].ColumnSpan = 2;
                header[1].Text = "首道船";
                header[2].ColumnSpan = 3;
                header[2].Text = "公园票";
                header[3].ColumnSpan = 2;
                header[3].Text = "颐内一道船";
                header[4].ColumnSpan = 2;
                header[4].Text = "颐内二道船";
                header[5].ColumnSpan = 2;
                header[5].Text = "</th><tr>";

                //第二行
                header[6].Text = "开票时间";
                header[7].Text = "结账时间";
                header[8].Text = "流水号";
                header[9].Text = "社名";
                header[10].Text = "导游";

                header[11].Text = "人数";
                header[12].Text = "金额";

                header[13].Text = "普通票";
                header[14].Text = "学生票";
                header[15].Text = "金额";

                header[16].Text = "人数";
                header[17].Text = "金额";

                header[18].Text = "人数";
                header[19].Text = "金额";

                header[20].Text = "非签单总金额";
                header[21].Text = "单号";
                

                Dictionary<int, int> mergeCellNums = new Dictionary<int, int>();
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    mergeCellNums.Add(i, 2);
                }
                string filename = "";
                saveFileDialog1.FileName = DateTime.Now.ToString("yyyyMMdd-HHmmss") + "-日统计出票(非签单)报表.xlsx";
                saveFileDialog1.Filter = " xls files(*.xls)|*.xlsx|All files(*.*)|*.*";
                saveFileDialog1.FilterIndex = 2;
                saveFileDialog1.RestoreDirectory = true;
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    filename = saveFileDialog1.FileName.ToString();
                    try
                    {
                        dt.Columns[14].SetOrdinal(0);
                        dt.Columns[15].SetOrdinal(1);
                        ToExcel.DataTable2Excel(dt, header, filename, mergeCellNums, 0);
                        MessageBox.Show("导出成功!");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                    }
                }
            }
        }

        private void ExportSalesSigned()
        {
            DataTable dt = (DataTable)rowMergeView_Signed.DataSource;
            if (dt == null || dt.Rows.Count == 0)
            {
                MessageBox.Show("无数据");
            }
            else
            {
                System.Web.UI.WebControls.TableCell[] header = new System.Web.UI.WebControls.TableCell[10];
                for (int i = 0; i < header.Length; i++)
                {
                    header[i] = new System.Web.UI.WebControls.TableCell();
                }
                header[0].Text = "开票时间";
                header[1].Text = "结账时间";
                header[2].Text = "流水号";
                header[3].Text = "社名";
                header[4].Text = "导游";
                header[5].Text = "签单号";

                header[6].Text = "人数";
                header[7].Text = "签单人数";

                header[8].Text = "签单金额";
                header[9].Text = "单号";

                Dictionary<int, int> mergeCellNums = new Dictionary<int, int>();
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    mergeCellNums.Add(i, 2);
                }
                string filename = "";
                saveFileDialog1.FileName = DateTime.Now.ToString("yyyyMMdd-HHmmss") + "-日统计出票(签单)报表.xlsx";
                saveFileDialog1.Filter = " xls files(*.xls)|*.xlsx|All files(*.*)|*.*";
                saveFileDialog1.FilterIndex = 2;
                saveFileDialog1.RestoreDirectory = true;
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    filename = saveFileDialog1.FileName.ToString();
                    try
                    {
                        dt.Columns[8].SetOrdinal(0);
                        dt.Columns[9].SetOrdinal(1);
                        ToExcel.DataTable2Excel(dt, header, filename, mergeCellNums, 0);
                        MessageBox.Show("导出成功!");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                    }
                }
            }
        }

        private void ExportBacks()
        {
            DataTable dt = (DataTable)rowMergeView_Back.DataSource;
            if (dt == null || dt.Rows.Count == 0)
            {
                MessageBox.Show("无数据");
            }
            else
            {
                System.Web.UI.WebControls.TableCell[] header = new System.Web.UI.WebControls.TableCell[21];
                for (int i = 0; i < header.Length; i++)
                {
                    header[i] = new System.Web.UI.WebControls.TableCell();
                }
                header[0].ColumnSpan = 5;
                header[0].Text = "";
                header[1].ColumnSpan = 2;
                header[1].Text = "首道船";
                header[2].ColumnSpan = 3;
                header[2].Text = "公园票";
                header[3].ColumnSpan = 2;
                header[3].Text = "颐内一道船";
                header[4].ColumnSpan = 2;
                header[4].Text = "颐内二道船";
                header[5].ColumnSpan = 1;
                header[5].Text = "</th><tr>";

                //第二行
                header[6].Text = "退票时间";
                header[7].Text = "ID";
                header[8].Text = "原始流水号";
                header[9].Text = "社名";
                header[10].Text = "导游";

                header[11].Text = "人数";
                header[12].Text = "金额";

                header[13].Text = "普通票";
                header[14].Text = "学生票";
                header[15].Text = "金额";

                header[16].Text = "人数";
                header[17].Text = "金额";

                header[18].Text = "人数";
                header[19].Text = "金额";

                header[20].Text = "退单总金额";
                

                Dictionary<int, int> mergeCellNums = new Dictionary<int, int>();
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    mergeCellNums.Add(i, 2);
                }
                string filename = "";
                saveFileDialog1.FileName = DateTime.Now.ToString("yyyyMMdd-HHmmss") + "-日统计退票报表.xlsx";
                saveFileDialog1.Filter = " xls files(*.xls)|*.xlsx|All files(*.*)|*.*";
                saveFileDialog1.FilterIndex = 2;
                saveFileDialog1.RestoreDirectory = true;
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    filename = saveFileDialog1.FileName.ToString();
                    try
                    {
                        dt.Columns[14].SetOrdinal(0);
                        ToExcel.DataTable2Excel(dt, header, filename, mergeCellNums, 0);
                        MessageBox.Show("导出成功!");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                    }
                }
            }
        }
        //回车事件（下一个）
        private void PressEnter_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                System.Windows.Forms.SendKeys.Send("{TAB}");
            }
        }

        private void PressEnter2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btn_Search_Click(sender, e);
            }
        }

        private void checkBox_TimeSpan_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_TimeSpan.Checked)
            {
                this.dTP_Start.CustomFormat = "yyyy-MM-dd HH:mm:ss";
                this.dTP_End.CustomFormat = "yyyy-MM-dd HH:mm:ss";
            }
            else
            {
                this.dTP_Start.CustomFormat = "yyyy-MM-dd";
                this.dTP_End.CustomFormat = "yyyy-MM-dd";
            }
        }

        private void printerComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (printerComboBox.SelectedItem != null) //判断是否有选中值
            {
                if (Common.Externs.SetDefaultPrinter(printerComboBox.SelectedItem.ToString())) //设置默认打印机
                {
                    MessageBox.Show(printerComboBox.SelectedItem.ToString() + "设置为默认打印机成功！");
                }
                else
                {
                    MessageBox.Show(printerComboBox.SelectedItem.ToString() + "设置为默认打印机失败！");
                }
            }
        }

        private void InitprinterComboBox()
        {
            List<String> list = Common.PrintDataGridView.GetLocalPrinters(); //获得系统中的打印机列表
            foreach (String s in list)
            {
                printerComboBox.Items.Add(s); //将打印机名称添加到下拉框中
            }
        }

        
    }
}
